package com.isoft.controller;

import com.isoft.pojo.dto.PageQueryTableDTO;
import com.isoft.pojo.dto.TableCreateDTO;
import com.isoft.pojo.vo.ImportResultVO;
import com.isoft.pojo.vo.Table1VO;
import com.isoft.result.PageResult;
import com.isoft.result.Result;
import com.isoft.service.TableService;
import com.isoft.pojo.vo.TableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;

@RestController
@Slf4j
@RequestMapping("/admin/tables")
@Tag(name = "表结构管理接口")
public class TableController {

    @Autowired
    private TableService tableService;

    /**
     * 获取表列表（可分页、按系统筛选）
     * @param systemId 系统ID
     * @param page 当前页码
     * @param pageSize 每页条数
     * @param searchKey 搜索关键词
     * @return 分页结果
     */
    @Operation(summary = "获取表列表")
    @Parameters({
            @Parameter(name = "systemId", description = "系统ID"),
            @Parameter(name = "page", description = "当前页码", example = "1"),
            @Parameter(name = "pageSize", description = "每页条数", example = "10"),
            @Parameter(name = "searchKey", description = "搜索关键词（表名/编码）")
    })
    @GetMapping
    public Result<PageResult<Table1VO>> getTableList(
            @RequestParam(required = false) String systemId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String searchKey) {

        PageQueryTableDTO dto = new PageQueryTableDTO();
        dto.setSystemId(systemId);
        dto.setPage(page);
        dto.setSize(pageSize);
        dto.setSearchKey(searchKey);

        return tableService.getTableList(dto);
    }
    @Operation(summary = "获取表详情")
    @GetMapping("/{id}")
    public Result<TableVO> getTableByIds(@PathVariable String id) {
        log.info("获取表详情");
        log.info("参数：{}", id);
        return tableService.getTableByIds(id);
    }
    @Operation(summary = "新增表")
    @PostMapping("/add")
    public Result<?> addTable(@RequestBody TableCreateDTO dto) {
        try {
            boolean success = tableService.createTable(dto);
            if (success) {
                // 创建成功结果并设置消息
                Result<?> result = Result.success();
                result.setMessage("表创建成功");
                return result;
            }
            // 创建带错误消息的失败结果
            return Result.error("表创建失败");
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }
    @Operation(summary = "导入SQL文件解析表结构")
    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<ImportResultVO> importSql(
            @RequestParam("file") MultipartFile file,
            @RequestParam("systemId") String systemId,
            @RequestParam(value = "override", defaultValue = "false") Boolean override) {

        try {
            ImportResultVO result = tableService.importSql(file, systemId, override);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("导入失败: " + e.getMessage());
        }
    }
}
